package com.example.myproject;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class NewsBaseAdapter extends BaseAdapter {
    Context context;
    List<NewsBean.DataBean.FeedlistBean> feedlist;
    public NewsBaseAdapter(Context context, List<NewsBean.DataBean.FeedlistBean> feedlist) {
        this.context = context;
        this.feedlist = feedlist;
    }
    @Override
    public int getCount() { return feedlist.size(); }
    @Override
    public Object getItem(int position) { return feedlist.get(position); }
    @Override
    public long getItemId(int position) { return position; }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VH vh = null;
        // 是否有能够重复使用的view
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_news,null);
            vh = new VH(convertView);
            convertView.setTag(vh);
        }else {
            vh = (VH) convertView.getTag();
        }
        NewsBean.DataBean.FeedlistBean bean = feedlist.get(position);
        NewsBean.DataBean.FeedlistBean.ItemsBeanX itemsBeanX = bean.getItems().get(0);
        vh.desTv.setText(itemsBeanX.getDesc());
        vh.titleTv.setText(itemsBeanX.getTitle());
        long ltime = itemsBeanX.getTime();
        //时间格式化
        Date date = new Date(ltime);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        vh.timeTv.setText(format);
        int picnum = 0;   //计算显示几张图
        String img = itemsBeanX.getImg();
        if (!TextUtils.isEmpty(img)) {
            picnum++;
            vh.iv1.setVisibility(View.VISIBLE);
            Picasso.with(context).load(img).into(vh.iv1);
        }else{
            vh.iv1.setVisibility(View.GONE);
        }

        String img02 = itemsBeanX.getImg02();
        if (!TextUtils.isEmpty(img02)) {
            picnum++;
            vh.iv2.setVisibility(View.VISIBLE);
            Picasso.with(context).load(img02).into(vh.iv2);
        }else{
            vh.iv2.setVisibility(View.GONE);
        }

        String img03 = itemsBeanX.getImg03();
        if (!TextUtils.isEmpty(img03)) {
            picnum++;
            vh.iv3.setVisibility(View.VISIBLE);
            Picasso.with(context).load(img03).into(vh.iv3);
        }else{
            vh.iv3.setVisibility(View.GONE);
        }
// 有3张图，让3张图的高度一致
        if (picnum == 3) {
            ViewGroup.LayoutParams params = vh.iv1.getLayoutParams();
            params.height = 260;
            vh.iv1.setLayoutParams(params);
            vh.iv2.setLayoutParams(params);
            vh.iv3.setLayoutParams(params);
        }

        return convertView;
    }
    class VH{
        TextView titleTv,desTv,timeTv;
        ImageView iv1,iv2,iv3;
        public VH(View v){
            titleTv = v.findViewById(R.id.item_tv_title);
            desTv = v.findViewById(R.id.item_tv_des);
            timeTv = v.findViewById(R.id.item_tv_time);
            iv1 = v.findViewById(R.id.item_iv1);
            iv2 = v.findViewById(R.id.item_iv2);
            iv3 = v.findViewById(R.id.item_iv3);
        }
    }
}
